// pkg_wo/pages/orderInfo/orderInfo.js
const app = getApp();
import request from '../../../utils/http.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderV:1,//新旧订单类型：1，20230525的订单版本，老的版本，2,20231109的订单版本，新版本
    orderFormId: 0, //订单ID
    personId: 0,
    shopId: 0,

    userName: '', //联系人
    provinceName: '', //省
    cityName: '',
    countyName: '', //区
    detailInfo: '', //详细地址
    telNumber: '', //联系电话

    dingDanHao: '',
    dingDanZhuangTai: 0, //订单状态：0待支付， 1待发货，2待收货，3已收货，4申请退货，5退货成功不发佣金，6已收货变成完成，发放佣金,正常的购买流程 7取消订单申请，8取消订单成功
    zongChanPinJinE: 0,
    zongYunFen: 0,
    zongJiaoYiJinE: 0,
    zhiFuRiQi: '',
    chuangJianRiQi: '',
    wuLiuFaHuoRiQi: '',
    wuLiuShouHuoRiQi: '',
    wuLiuGongSiId: 0,
    wuLiuDanHao: '',
    wuLiuGongSiMingCheng: '',
    chanPinList: [],
    statusList: [],
    statusListLenght: 0,
    pathSign: 1, //1自己看订单，2 店主看订单

    IsZhongChou: 1, //1非众筹订单，2众筹订单
    payFangShi: 1,
    payFangShiStr: '',
    xuNiZhuangTai: 0,
    zengSongStatus: 0,
    kuaiDiFangShi: 1,
    dingDanLeiXing: 1, //订单类型：1正常产品订单，2服务包订单，3普通商品众筹订单，4众筹服务包订单, 101正常产品订单-包含虚拟商品，102服务包订单-包含虚拟商品，103普通商品众筹订单-包含虚拟商品，104众筹服务包订单-包含虚拟商品,
    actionUrl:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //  console.log("options:"+JSON.stringify(options))

    this.setData({
      personId: wx.getStorageSync(app.globalData.PERSON_ID),
      orderFormId: options.orderFormId,
      shopId: wx.getStorageSync(app.globalData.SHOPID),
      pathSign: options.pathSign,
      orderV:options.orderV,
      actionUrl:options.orderV==1?'dingDan':'OrderForm20231109'
    })

    this.getOrderInfo()

    // if(this.data.orderV==2){

    //   this.getOrderInfo_new()

    // }else{

    //   this.getOrderInfo_old()
    // }

  },

  //获取订单详情
  getOrderInfo: function () {
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    var that = this
    var url=that.data.actionUrl+'/chaKanOneOrderForm'
    let data = {
      orderFormId: that.data.orderFormId,
    }
    request.get(url, data, res => {

      console.log("订单详情:" + JSON.stringify(res.data))
      that.setData({
        dingDanHao: res.data.dingDanHao,
        dingDanZhuangTai: res.data.dingDanZhuangTai,
        zongChanPinJinE: res.data.zongChanPinJinE,
        zongYunFen: res.data.zongYunFen,
        zongJiaoYiJinE: res.data.zongJiaoYiJinE,
        zhiFuRiQi: res.data.zhiFuRiQi,
        chuangJianRiQi: res.data.chuangJianRiQi,
        wuLiuFaHuoRiQi: res.data.wuLiuFaHuoRiQi,
        wuLiuShouHuoRiQi: res.data.wuLiuShouHuoRiQi,
        wuLiuGongSiId: res.data.wuLiuGongSiId,
        wuLiuDanHao: res.data.wuLiuDanHao,
        wuLiuGongSiMingCheng: res.data.wuLiuGongSiMingCheng,
        chanPinList: res.data.chanPinList, //shangPinZhuangTai = 1;//1正常产品购买，2正常商品的赠品，3服务包正常产品，4服务包怎送产品
        statusList: res.data.statusList,
        statusListLenght: res.data.statusList.length,
        userName: res.data.wxUserName,
        provinceName: res.data.wxProvinceName,
        cityName: res.data.wxCityName,
        countyName: res.data.wxCountyName,
        detailInfo: res.data.wxDetailInfo,
        telNumber: res.data.wxTelNumber,
        IsZhongChou: res.data.IsZhongChou,
        payFangShiStr: res.data.payFangShiStr,
        payFangShi: res.data.payFangShi,
        xuNiZhuangTai: res.data.xuNiZhuangTai, //产品虚拟状态：1实物，2虚拟商品
        zengSongStatus: res.data.zengSongStatus, //当前订单赠送状态：1正常支付订单，2订单父订单，3赠送订单
        dingDanLeiXing: res.data.dingDanLeiXing,
        kuaiDiFangShi: res.data.kuaiDiFangShi,
        weiXinZhiFuJinE:res.data.weiXinZhiFuJinE,
        youHuiQuanDiKouJinE:res.data.youHuiQuanDiKouJinE

      })

      wx.hideLoading({
        success: (res) => {},
      })

    })
  },



  //待支付订单执行支付
  toPay: function () {
    var that = this
    var url=that.data.actionUrl+'/againPayOrderForm'
    let data = {
      openId: wx.getStorageSync(app.globalData.OPENID),
      orderFormId: that.data.orderFormId,

    }
    request.get(url, data, res => {

      // console.log("提交订单:"+JSON.stringify(res.data))
      wx.hideLoading()

      if (res.data.status == 1) {
        wx.requestPayment({
          timeStamp: res.data.map.timeStamp,
          nonceStr: res.data.map.nonce_str,
          package: 'prepay_id=' + res.data.map.prepay_id,
          signType: 'MD5',
          paySign: res.data.map.paySign,
          success: function (ress) {

            that.syncData()
            wx.showToast({
              title: "支付成功！", // 提示的内容
              icon: "none", // 图标，默认success
              duration: 1000, // 提示的延迟时间，默认1500
              mask: false, // 是否显示透明蒙层，防止触摸穿透
              complete: function () {
                let pages = getCurrentPages(); //定义上页面
                let prevPage = pages[pages.length - 2];
                prevPage.setData({
                  tabIndex: 2,
                })

                setTimeout(function () {
                  wx.navigateBack({
                    delta: 1,
                  })
                }, 1000)
              }
            })

          },
          fail: function (ress) {
            wx.showToast({
              title: "支付失败！", // 提示的内容
              icon: "none", // 图标，默认success
              duration: 3000, // 提示的延迟时间，默认1500
              mask: false, // 是否显示透明蒙层，防止触摸穿透
            })

            that.setData({
              isClick: false
            })
          },
          complete: function (ress) {
            //console.log("payComplete:" + JSON.stringify(res.data))
          }
        });

        // console.log("that.data.dingDanHao:"+that.data.dingDanHao)

        // that.zhifu(res.data.dingDanHao);
      } else { //订单生成失败

        wx.showToast({
          title: "订单生成失败！", // 提示的内容
          icon: "none", // 图标，默认success
          duration: 3000, // 提示的延迟时间，默认1500
          mask: false, // 是否显示透明蒙层，防止触摸穿透
          complete: function () {
            that.setData({
              isClick: false
            })
          }
        })
      }
    })

  },



  
  //登录同步健康数据
  syncData(){
    request.get('qiTa/tongBuJiuZhenRenAndGouMaiChanPin', null, res => {
      console.log("下单同步数据成功" )
    })
  },

  //虚拟支付
  zhifu: function (dingDanHao) {
    var that = this
    let data = {
      dingDanHao: dingDanHao,
    }
    request.get('OrderForm20231109/xiuNiZhiFu', data, res => {
      wx.hideLoading();
      wx.showToast({
        title: '支付成功', // 提示的内容
        icon: "none", // 图标，默认success
        duration: 1500, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
      // wx.reLaunch({
      //   url: '/pkg_wo/pages/allOrders/allOrders?index=1',
      // })
    })
  },

  //取消待支付订单
  toDelOrder: function () {

    var that = this
    var url=that.data.actionUrl+'/deleteOrderForm'
    let data = {
      orderFormId: that.data.orderFormId,
    }
    request.get(url, data, res => {

      // console.log("提交订单:"+JSON.stringify(res.data))
      // wx.hideLoading()

      let pages = getCurrentPages(); //定义上页面
      let prevPage = pages[pages.length - 2];
      prevPage.setData({
        tabIndex: 1,
      })

      // setTimeout(function () {
      wx.navigateBack({
        delta: 1,
      })
      // }, 1000)


    })
  },


  //查看物流
  getWuLiuInfo: function () {
    wx.navigateTo({
      url: '/pkg_wo/pages/wuLiu/wuLiu?orderFormId=' + this.data.orderFormId +'&orderV='+this.data.orderV,
    })
  },

  //确认收货
  bindQueRenTap: function () {
    var that = this
    wx.showModal({

      title: '确认收货',
      content: '是否确认已收到货？',
      showCancel: true, //是否显示取消按钮
      cancelText: "否", //默认是“取消”
      cancelColor: '#696868', //取消文字的颜色
      confirmText: "是", //默认是“确定”
      confirmColor: '#f92b3a', //确定文字的颜色
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {

          let data = {
            orderFormId: that.data.orderFormId,
          }
          var url=that.data.actionUrl+'/queRenShouHuo'

          request.get(url, data, res => {
            if (res.data.status == 1) {

              that.getOrderInfo()
            } else {
              wx.showToast({
                title: "确认失败，请联系技术人员！", // 提示的内容
                icon: "none", // 图标，默认success
                duration: 3000, // 提示的延迟时间，默认1500
                mask: false, // 是否显示透明蒙层，防止触摸穿透
              })
            }

          }, err => {
            wx.showToast({
              title: '处理失败，请联系技术人员', // 提示的内容
              icon: "none", // 图标，默认success
              duration: 1000, // 提示的延迟时间，默认1500
              mask: false, // 是否显示透明蒙层，防止触摸穿透
            })

          })



        }
      },
      fail: function (res) {}, //接口调用失败的回调函数
      complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })


  },

  //申请退款
  toTuiKuanInfo: function (e) {
    var type = e.currentTarget.dataset.index;
    var jine = 0;

    if (this.data.IsZhongChou == 2) {

      wx.showModal({
        title: '提示信息',
        content: '亲，众筹订单，不可以退款！',
        showCancel: false,
        success(res) {}

      })

    } else {
      // if (type == 1) {
      //   jine = this.data.zongJiaoYiJinE
      // } else {
      //   jine = this.data.zongChanPinJinE
      // }
      wx.navigateTo({
        url: '/pkg_wo/pages/tuiKuan/tuiKuan?orderFormId=' + this.data.orderFormId + '&pric=' + this.data.zongJiaoYiJinE + '&type=' + type,
      })
    }

  },


  //撤销取消订单
  cancleTuiKuan: function () {
    var that = this
    let data = {
      orderFormId: that.data.orderFormId,
    }

    var url=that.data.actionUrl+'/regainNormalOrderForm'

    request.get(url, data, res => {

      that.getOrderInfo()
      
    }, err => {
      wx.showToast({
        title: '处理失败，请联系技术人员', // 提示的内容
        icon: "none", // 图标，默认success
        duration: 1000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })

    })

  },


  //放大图片
  clickImg: function (e) {
    var imgUrl = e.currentTarget.dataset.url;
    wx.previewImage({
      urls: [imgUrl], //需要预览的图片http链接列表，注意是数组
      current: '', // 当前显示图片的http链接，默认是第一个
      success: function (res) {},
      fail: function (res) {},
      complete: function (res) {},
    })
  },


  //复制内容
  setClipboardDataAction: function (e) {
    var index = e.currentTarget.dataset.index;
    var copystr = '';
    if (index == 1) {
      copystr = this.data.dingDanHao
    } else {
      copystr = this.data.wuLiuDanHao
    }

    wx.setClipboardData({
      data: copystr,
      success(res) {
        wx.getClipboardData({
          success(res) {
            // console.log(res.data) // data
          }
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})